﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AMS.DataAccess.Sql
{
    /// <summary>
    /// 构造Sql语句的类。
    /// 每次调用Add，都将开启新的一行
    /// </summary>
    public class SqlBuilder : SqlStatement
    {
        /// <summary>
        /// 构造Sql语句的类
        /// </summary>
        public SqlBuilder()
        {
            sbSql = new StringBuilder();
        }

        /// <summary>
        /// 构造Sql语句的类
        /// </summary>
        /// <param name="tableName">表名</param>
        public SqlBuilder(string tableName)
            : this()
        {
            TableName = tableName;
        }

        /// <summary>
        /// 构造Sql语句的类
        /// </summary>
        /// <param name="compactMode">是否紧凑模式</param>
        public SqlBuilder(bool compactMode)
            : this()
        {
            CompactMode = compactMode;
        }

        /// <summary>
        /// 保存SQL语句的内容
        /// </summary>
        private StringBuilder sbSql;

        /// <summary>
        /// 获取表名
        /// </summary>
        public string TableName { get; private set; }

        /// <summary>
        /// 在紧凑模式下，需要在新加内容之前添加一个空格，以构成一个合法的SQL语句
        /// </summary>
        /// <param name="testSqlPart"></param>
        private void Prepare(string testSqlPart)
        {
            if (!CompactMode || sbSql.Length == 0)
            {
                return;
            }

            if (string.IsNullOrEmpty(testSqlPart) || !char.IsWhiteSpace(testSqlPart[0]))
            {
                sbSql.Append(" ");
            }
        }

        /// <summary>
        /// 添加一段SQL语句
        /// </summary>
        /// <param name="sql">sql语句片段</param>
        public void Add(string sql)
        {
            Prepare(sql);
            NL(sbSql.Append(sql));
        }

        /// <summary>
        /// 添加一段SQL语句。通过sql和expr组成一个连接的表达式。
        /// </summary>
        /// <example>
        /// .Add("    AND ", new SqlExpression("AID", 1))
        /// </example>
        /// <param name="sql">sql语句片段</param>
        /// <param name="expr">表达式</param>
        public void Add(string sql, SqlExpression expr)
        {
            Prepare(sql);
            sbSql.Append(sql);
            NL(sbSql.Append(expr.ToString()));
        }

        /// <summary>
        /// 添加两段SQL，对两段SQL不进行任何的处理。
        /// </summary>
        /// <param name="sql1"></param>
        /// <param name="sql2"></param>
        public void Add(string sql1, string sql2)
        {
            Prepare(sql1);
            sbSql.Append(sql1);
            NL(sbSql.Append(sql2));
        }

        /// <summary>
        /// 添加一个Sql表达式，对表达式不进行处理。
        /// </summary>
        /// <param name="expr"></param>
        public void Add(SqlExpression expr)
        {
            Prepare(null);
            NL(sbSql.Append(expr.ToString()));
        }

        /// <summary>
        /// 输入表达式 SQL 语句
        /// </summary>
        /// <returns>转换后的 SQL 语句</returns>
        public override string ToString()
        {
            return sbSql.ToString();
        }
    }
}
